<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends BIT_Controller
{
	function __construct() {
		parent::__construct();
	}
	
	public function set_item($id = false, $quantity = 1) {
		$result = false;

		if($id !== false) {
			
			$this->load->library('cart_handler');
			$rowid = $this->cart_handler->get_rowid('prod_' . $id);
			
			if($rowid) {

				// Remove product?
				if(intval($quantity) === 0)
					$result = $this->cart_handler->update_item($rowid, 0) ? true : false;
				// No, update product
				else
					$result = $this->cart_handler->update_item($rowid, $quantity) ? true : false;

			} else {
				$result = $this->cart_handler->add_item($id, $quantity) ? true : false;
			}
		}
		echo json_encode(array('status' => $result));	
	}

	public function get_recipes(){
		print json_encode($this->recept->get_json_recipes());
	}

	public function get_cart(){
		$this->load->library('cart_handler');

		$cart = new stdClass();
		$cart->items = $this->cart_handler->get_contents();
		$cart->count = $this->cart_handler->get_count();
		$cart->totalprice = $this->cart_handler->get_total();

		echo json_encode($cart);
	}
}